package com.hongmeng.operater.service.aliyun.impl;

import com.hongmeng.common.constant.Constants;
import com.hongmeng.common.core.domain.AjaxResult;
import com.hongmeng.operater.domain.HmFile;
import com.hongmeng.operater.domain.aliyun.FileUploadCompleteEntity;
import com.hongmeng.operater.domain.aliyun.VodVideoEntity;
import com.hongmeng.operater.domain.newsAndVideo.HmVideoFile;
import com.hongmeng.operater.mapper.HmFileMapper;
import com.hongmeng.operater.mapper.newsAndVideo.HmVideoFileExtMapper;
import com.hongmeng.operater.mapper.newsAndVideo.HmVideoFileMapper;
import com.hongmeng.operater.service.aliyun.FileUploadCompleteListener;
import com.hongmeng.operater.service.aliyun.IVodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import static com.hongmeng.common.utils.file.FileUtils.checkFileSize;

@Component
@Slf4j
public class FileUploadCompleteListenerImpl extends FileUploadCompleteListener {

    @Autowired
    private HmVideoFileMapper hmVideoFileMapper;

    @Autowired
    private HmVideoFileExtMapper hmVideoFileExtMapper;

    @Autowired
    private HmFileMapper hmFileMapper;

    @Autowired
    private IVodService vodService;

    /**
     * 视频上传完成回调
     * @param iVodDto iVodDto
     * @return BaseResponse
     */
    @Transactional
    @Override
    public AjaxResult notify(FileUploadCompleteEntity iVodDto) {
        //通过视频id 查询数据是否已存在，是则更新源文件url。否则不操作
        HmVideoFile queryVideoFile = hmVideoFileExtMapper.selectByVideoId(iVodDto.getVideoId());
        if (queryVideoFile==null){
            return AjaxResult.success(iVodDto);
        }
        HmFile queryFile = hmFileMapper.selectHmFileById(queryVideoFile.getVideoFileId());
        if (queryFile==null){
            return AjaxResult.success(iVodDto);
        }
        //更新视频信息
        try{
            //获取视频时长,视频大小
            AjaxResult videoInfo = vodService.getVodOriginalFileById(iVodDto.getVideoId());
            VodVideoEntity videoEntity = (VodVideoEntity) videoInfo.get("data");
            queryFile.setFileName(videoEntity.getVideoTitle());
            queryVideoFile.setVideoTime(videoEntity.getDuration());
            log.info("获取到的视频信息:"+videoEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //视频大小存储单位KB
        queryFile.setFileSize(checkFileSize(iVodDto.getSize(),Constants.FILE_UNIT.KB));
        queryFile.setFileUrl(iVodDto.getFileUrl());
        queryVideoFile.setVideoSize(String.valueOf(checkFileSize(iVodDto.getSize(),Constants.FILE_UNIT.KB)));
        queryVideoFile.setVideoSourceUrl(iVodDto.getFileUrl());
        int videoCount = hmVideoFileMapper.updateHmVideoFile(queryVideoFile);
        int fileCount = hmFileMapper.updateHmFile(queryFile);
        log.info("更新视频文件信息共{}条，文件信息共{}条，视频id为:{},视频文件id为:{}",videoCount,fileCount,queryVideoFile.getVideoId(),queryVideoFile.getVideoFileId());
        return AjaxResult.success(queryVideoFile);
    }
}
